package com.monitoring.management.facade.facede_imp;

import com.monitoring.management.common.Response;
import com.monitoring.management.common.exceptions.UserIsExistedException;
import com.monitoring.management.common.exceptions.UserNotExistedException;
import com.monitoring.management.common.exceptions.UserNotValidException;
import com.monitoring.management.common.exceptions.UserPasswordException;
import com.monitoring.management.common.page.Page;
import com.monitoring.management.common.utils.assembler.UserAssembler;
import com.monitoring.management.core.command.LoginCommand;
import com.monitoring.management.core.command.RegisterCommand;
import com.monitoring.management.core.command.query.QueryUserCommand;
import com.monitoring.management.core.dto.UserDTO;
import com.monitoring.management.core.service.IUserService;
import com.monitoring.management.facade.UserFacede;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by liujunnan on 2017/6/6.
 */
@Named
public class UserFacedeImpl implements UserFacede {

    transient static final Logger LOGGER = LoggerFactory.getLogger(UserFacedeImpl.class);
    @Inject
    IUserService userService;

    public Response login(LoginCommand loginCommand) {

        Response response = new Response();
        try {
            UserDTO userDTO = userService.login(UserAssembler.toUserDTO(loginCommand));
            if (userDTO != null) {
                //将用户密码置空，不传给前台
                userDTO.setPassword("No UserDTO Password!");
                response.setCode(Response.CodeEnum.SUCCESS.code);
                response.setMsg("登录成功");
                response.setData(userDTO);
            } else {
                response.setCode(Response.CodeEnum.FAILED.code);
                response.setMsg("登录失败");
                response.setData(null);
            }
            return response;
        } catch (UserNotExistedException e) {
            LOGGER.error(e.getMessage(), e);
            response.setCode(Response.CodeEnum.FAILED.code);
            response.setMsg(e.getMessage());
            return response;
        } catch (UserPasswordException e) {
            LOGGER.error(e.getMessage(), e);
            response.setCode(Response.CodeEnum.FAILED.code);
            response.setMsg(e.getMessage());
            return response;
        } catch (UserNotValidException e) {
            LOGGER.error(e.getMessage(), e);
            response.setCode(Response.CodeEnum.FAILED.code);
            response.setMsg(e.getMessage());
            return response;
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            response.setCode(Response.CodeEnum.UNKNOWN.code);
            response.setMsg(e.getMessage());
            return response;
        }

    }

    public Response register(RegisterCommand registerCommand) {
        Response response = new Response();
        try {
            int flag = userService.register(UserAssembler.toUserDTO(registerCommand));
            if (flag > 0) {   //注册成功
                response.setCode(Response.CodeEnum.SUCCESS.code);
                response.setMsg("注册成功");
                response.setData("flag");
            } else {
                response.setCode(Response.CodeEnum.FAILED.code);
                response.setMsg("注册失败");
                response.setData("flag");
            }
        } catch (UserIsExistedException e) {
            LOGGER.error(e.getMessage(), e);
            response.setCode(Response.CodeEnum.AUTHOR.code);
            response.setMsg(e.getMessage());
            return response;
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            response.setCode(Response.CodeEnum.UNKNOWN.code);
            response.setMsg(e.getMessage());
            return response;
        }
        return response;
    }

    public Response pageUser(QueryUserCommand queryUserCommand) {
        Response response = new Response();
        try {
            Page<UserDTO> userDTO = userService.pageUser(queryUserCommand);
            if (userDTO != null) {
                response.setCode(Response.CodeEnum.SUCCESS.code);
                response.setMsg("查询成功");
                response.setData(userDTO.getDatas());
            } else {
                response.setCode(Response.CodeEnum.FAILED.code);
                response.setMsg("查询失败");
                response.setData(null);
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            response.setCode(Response.CodeEnum.UNKNOWN.code);
            response.setMsg(e.getMessage());
            return response;
        }

        return response;
    }

    public Response getExcelBySupply(String supplierId, HttpServletResponse res) throws IOException {
        Response response = new Response();
        HttpServletResponse excelRes = userService.getExcelBySupply(supplierId, res);

        if (excelRes != null) {
            response.setData("");
            response.setCode(Response.CodeEnum.SUCCESS.code);
            response.setMsg("获取Excel成功！");
        } else {
            response.setCode(Response.CodeEnum.FAILED.code);
            response.setMsg("获取Excel失败！");
            response.setData("");
        }
        return response;
    }

    public Response usersListSave(String filePath, String userId) {

        Response response = new Response();

        List<String> errorList = userService.usersListSave(filePath, userId);

        if (errorList.size() == 0) {
            response.setData("");
            response.setCode(Response.CodeEnum.SUCCESS.code);
            response.setMsg("更新库存成功！");
        } else {
            String Message = "";
            for (String s : errorList) {
                String[] error = s.split("-");
                if (error[1] == "1" || error[1].equals("1")) {
                    Message = Message + "序号" + error[0] + "：品牌错误（不为西门子）" + ",";
                } else if (error[1] == "2" || error[1].equals("2")) {
                    Message = Message + "序号" + error[0] + "：价格或者数量不为数字" + ",";
                } else if (error[1] == "3" || error[1].equals("3")) {
                    Message = Message + "表内容与模板不符合";
                } else {
                    Message = error[0];
                }
            }
            response.setCode(Response.CodeEnum.FAILED.code);
            response.setMsg("更新库存失败！");
            response.setData(Message);
        }
        return response;
    }

    public Response ListSave(String filePath) {

        Response response = new Response();

        List<String> errorList = userService.ListSave(filePath);

        if (errorList.size() == 0) {
            response.setData("");
            response.setCode(Response.CodeEnum.SUCCESS.code);
            response.setMsg("更新库存成功！");
        } else {
            String Message = "";
            response.setCode(Response.CodeEnum.FAILED.code);
            response.setMsg("更新库存失败！");
            response.setData(Message);
        }
        return response;
    }
}
